package ar.edu.uba.fi.jmit.aspecter;

import java.io.File;

import javassist.ClassPool;

import ar.edu.uba.fi.jmit.aspecter.core.AdviceWeaver;
import ar.edu.uba.fi.jmit.aspecter.core.exception.AspecterException;
import ar.edu.uba.fi.jmit.aspecter.testUtils.SpyAdviceWeaver;
import ar.edu.uba.fi.jmit.aspecter.testUtils.SpyInstrumentor;


public class AdviceWeaverTestCase extends AspecterTestCase {

	public void testAdviceDirectory() {
		SpyAdviceWeaver weaver = new SpyAdviceWeaver();
		try {
			weaver.applyAdvice(new File("target/test-classes/ar"));
		} catch (AspecterException e) {
			fail(e);
		}
		assertTrue(weaver.advicedClass("ar.edu.uba.fi.jmit.aspecter.testUtils.PointTest3"));
		assertTrue(weaver.advicedClass("ar.edu.uba.fi.jmit.aspecter.testUtils.PointTest1"));
		assertTrue(weaver.advicedClass("ar.edu.uba.fi.jmit.aspecter.testUtils.PointTest2"));
		assertFalse(weaver.advicedClass("ar.edu.uba.fi.jmit.aspecter.core.AdviceWeaver"));
	}
	
	public void testAdvice() {
		AdviceWeaver weaver = new AdviceWeaver(ClassPool.getDefault(), "./target/test-classes");
		SpyInstrumentor instrumentor1 = new SpyInstrumentor();
		SpyInstrumentor instrumentor2 = new SpyInstrumentor();
		weaver.addInstrumentor(instrumentor1);
		weaver.addInstrumentor(instrumentor2);
		try {
			weaver.applyAdvice(new File("target/test-classes/ar"));
		} catch (AspecterException e) {
			fail(e);
		}
		assertTrue(instrumentor1.alteredClass("ar.edu.uba.fi.jmit.aspecter.AdviceWeaverTestCase"));
		assertTrue(instrumentor1.alteredClass("ar.edu.uba.fi.jmit.aspecter.testUtils.PointTest1"));
		assertTrue(instrumentor1.alteredClass("ar.edu.uba.fi.jmit.aspecter.testUtils.TestUtils"));
		assertTrue(instrumentor2.alteredClass("ar.edu.uba.fi.jmit.aspecter.AdviceWeaverTestCase"));
		assertTrue(instrumentor2.alteredClass("ar.edu.uba.fi.jmit.aspecter.testUtils.PointTest1"));
		assertTrue(instrumentor2.alteredClass("ar.edu.uba.fi.jmit.aspecter.testUtils.TestUtils"));
	}
}
